package com.skygrass.caesar.proxy.fallback;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

@Component
public class CaesarProxyFallback implements FallbackProvider
{
	private static final Logger log = LoggerFactory.getLogger(CaesarProxyFallback.class);

	@Override
	public String getRoute()
	{
		//		return "caesar.consumer";
		return null;
	}

	@Override
	public ClientHttpResponse fallbackResponse()
	{
		//		return new ClientHttpResponse() {
		//
		//			@Override
		//			public InputStream getBody() throws IOException
		//			{
		//				return new ByteArrayInputStream("now is ZullFallback".getBytes());
		//			}
		//
		//			@Override
		//			public HttpHeaders getHeaders()
		//			{
		//				HttpHeaders headers = new HttpHeaders();
		//				headers.setContentType(MediaType.APPLICATION_JSON);
		//				return headers;
		//			}
		//
		//			@Override
		//			public HttpStatus getStatusCode() throws IOException
		//			{
		//				return HttpStatus.OK;
		//			}
		//
		//			@Override
		//			public int getRawStatusCode() throws IOException
		//			{
		//				// TODO Auto-generated method stub
		//				return 200;
		//			}
		//
		//			@Override
		//			public String getStatusText() throws IOException
		//			{
		//				// TODO Auto-generated method stub
		//				return "OK";
		//			}
		//
		//			@Override
		//			public void close()
		//			{
		//			}
		//		};

		return null;
	}

	private final HttpStatus FALLBACK_HTTP_STATUS = HttpStatus.OK;

	@Override
	public ClientHttpResponse fallbackResponse(Throwable cause)
	{
		return new ClientHttpResponse() {

			@Override
			public InputStream getBody() throws IOException
			{
				log.info("-----------fallback-----------");
				return new ByteArrayInputStream(
						("now is ZullFallback：" + CaesarProxyFallback.this.getRoute()).getBytes());
			}

			@Override
			public HttpHeaders getHeaders()
			{
				HttpHeaders headers = new HttpHeaders();
				headers.setContentType(MediaType.APPLICATION_JSON);
				return headers;
			}

			@Override
			public HttpStatus getStatusCode() throws IOException
			{
				return FALLBACK_HTTP_STATUS;
			}

			@Override
			public int getRawStatusCode() throws IOException
			{
				// TODO Auto-generated method stub
				return FALLBACK_HTTP_STATUS.value();
			}

			@Override
			public String getStatusText() throws IOException
			{
				// TODO Auto-generated method stub
				return FALLBACK_HTTP_STATUS.getReasonPhrase();
			}

			@Override
			public void close()
			{
			}

		};
	}

}
